package com.santiao.youxiang.utils

import java.text.SimpleDateFormat
import java.util.Calendar
import java.util.Date
import kotlin.apply

object DemoUtils {
    fun formatDate(date: Date): String {
        val currentDate = Calendar.getInstance()
        val inputDate = Calendar.getInstance().apply { time = date }

        // 获取当前日期与输入日期的年月日信息
        val today = currentDate.get(Calendar.DAY_OF_YEAR) == inputDate.get(Calendar.DAY_OF_YEAR)
        val yesterday = currentDate.get(Calendar.DAY_OF_YEAR) - 1 == inputDate.get(Calendar.DAY_OF_YEAR)

        return when {
            today -> {
                // 如果是今天，返回 hh:mm 格式
                val sdf = SimpleDateFormat("HH:mm")
                sdf.format(date)
            }
            yesterday -> {
                // 如果是昨天，返回 "昨天"
                "昨天"
            }
            else -> {
                // 其他情况，返回 yyyy-MM-dd 格式
                val sdf = SimpleDateFormat("yyyy-MM-dd")
                sdf.format(date)
            }
        }
    }
}